using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace Microsoft.MSE.Data
{
    /// <summary>
    /// Summary description for DbMapper.
    /// </summary>
    public class DataMapper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static object ExecuteObject(Type type, DataReader reader)
        {
            ArrayList results = ExecuteCollection(type, reader);
            return (results.Count == 0) ? null : results[0];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static ArrayList ExecuteCollection(Type type, DataReader reader)
        {
            return ExecuteCollection(type, reader, false);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="reader"></param>
        /// <param name="useName"></param>
        /// <returns></returns>
        public static ArrayList ExecuteCollection(Type type, DataReader reader, bool useName)
        {
            ArrayList records = new ArrayList();

            while (reader.Read())
            {
                object obj = Activator.CreateInstance(type);
                reader.Fill(obj, useName);
                records.Add(obj);
            }

            return records;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="types"></param>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static ArrayList ExecuteCollection(Type[] types, DataReader reader)
        {
            return ExecuteCollection(types, reader, false);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="types"></param>
        /// <param name="reader"></param>
        /// <param name="useName"></param>
        /// <returns></returns>
        public static ArrayList ExecuteCollection(Type[] types, DataReader reader, bool useName)
        {
            // at least one arraylist will always be returned.
            ArrayList result = new ArrayList();

            for (int i = 0; i < types.Length; i++)
            {
                ArrayList records = new ArrayList();
                Type type = types[i];

                if (i > 0 && reader.NextResult() == false)
                {
                    result.Add(records);
                    continue;
                }

                while (reader.Read())
                {
                    object obj = Activator.CreateInstance(type);
                    reader.Fill(obj, useName);
                    records.Add(obj);
                }

                result.Add(records);
            }

            return result;
        }
    }
}